home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 41 (1994-09)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip / MegaDisc 41 (1994-09)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf / Programming / PASCAL_TUTES / tute5.pas < prev    next >
Pascal/Delphi Source File  |  1994-06-25  |  14KB  |  371 lines

  1.  
  2. {                              Tutorial Five
  3.  
  4.                        Real numbers don't eat quiche...
  5.  
  6.     Get a cup of coffee and take a deep breath, this one's going to
  7.     use at least three brain cells...
  8.  
  9.     In Tute2 we met variables for the first time.  So far we've seen
  10.     integer and string variables.  Are there others?  Yessir!  Below
  11.     are some simple variable types...
  12.  
  13.     ------------------------------
  14.  
  15.     INTEGER: Whole numbers.  There are several sub-categories of
  16.          integer, and they have the following definitions for
  17.          HiSpeed Pascal...
  18.  
  19.     ShortInt = -128..127;
  20.  
  21.     Byte = 0..255;
  22.  
  23.     Integer = -32768..32767;
  24.  
  25.     Word = 0..65535;
  26.  
  27.     LongInt = -2147483648..2147483647;
  28.  
  29.     Variables of the type ShortInt and Byte use one byte of memory,
  30.     Integers and Words use 2 bytes, and LongInts use 4 bytes.  Choosing
  31.     the correct variable type can make your program faster!  Which
  32.     integer type would be the most suitable for the variable "yourage"
  33.     seen in tute2?  Perhaps you'd like to go back and re-write that
  34.     program with a better variable type.  Or perhaps you'd rather
  35.     rassle a rooster?  Who knows?
  36.  
  37.     ------------------------------
  38.  
  39.     CHAR: 256 single characters make up the definition of the variable
  40.           type Char.  These are usually the ASCII set of characters,
  41.           and they comprise the following...
  42.  
  43. |   0 ^@ |  32    |  64  @ |  96  ` | 128  . | 160    | 192  À | 224  à |
  44. |   1 ^A |  33  ! |  65  A |  97  a | 129  . | 161  ¡ | 193  Á | 225  á |
  45. |   2 ^B |  34  " |  66  B |  98  b | 130  . | 162  ¢ | 194  Â | 226  â |
  46. |   3 ^C |  35  # |  67  C |  99  c | 131  . | 163  £ | 195  Ã | 227  ã |
  47. |   4 ^D |  36  $ |  68  D | 100  d | 132  . | 164  ¤ | 196  Ä | 228  ä |
  48. |   5 ^E |  37  % |  69  E | 101  e | 133  . | 165  ¥ | 197  Å | 229  å |
  49. |   6 ^F |  38  & |  70  F | 102  f | 134  . | 166  ¦ | 198  Æ | 230  æ |
  50. |   7 ^G |  39  ' |  71  G | 103  g | 135  . | 167  § | 199  Ç | 231  ç |
  51. |   8 ^H |  40  ( |  72  H | 104  h | 136  . | 168  ¨ | 200  È | 232  è |
  52. |   9 ^I |  41  ) |  73  I | 105  i | 137  . | 169  © | 201  É | 233  é |
  53. |  10 ^J |  42  * |  74  J | 106  j | 138  . | 170  ª | 202  Ê | 234  ê |
  54. |  11 ^K |  43  + |  75  K | 107  k | 139  . | 171  « | 203  Ë | 235  ë |
  55. |  12 ^L |  44  , |  76  L | 108  l | 140  . | 172  ¬ | 204  Ì | 236  ì |
  56. |  13 ^M |  45  - |  77  M | 109  m | 141  . | 173  ­ | 205  Í | 237  í |
  57. |  14 ^N |  46  . |  78  N | 110  n | 142  . | 174  ® | 206  Î | 238  î |
  58. |  15 ^O |  47  / |  79  O | 111  o | 143  . | 175  ¯ | 207  Ï | 239  ï |
  59. |  16 ^P |  48  0 |  80  P | 112  p | 144  . | 176  ° | 208  Ð | 240  ð |
  60. |  17 ^Q |  49  1 |  81  Q | 113  q | 145  . | 177  ± | 209  Ñ | 241  ñ |
  61. |  18 ^R |  50  2 |  82  R | 114  r | 146  . | 178  ² | 210  Ò | 242  ò |
  62. |  19 ^S |  51  3 |  83  S | 115  s | 147  . | 179  ³ | 211  Ó | 243  ó |
  63. |  20 ^T |  52  4 |  84  T | 116  t | 148  . | 180  ´ | 212  Ô | 244  ô |
  64. |  21 ^U |  53  5 |  85  U | 117  u | 149  . | 181  µ | 213  Õ | 245  õ |
  65. |  22 ^V |  54  6 |  86  V | 118  v | 150  . | 182  ¶ | 214  Ö | 246  ö |
  66. |  23 ^W |  55  7 |  87  W | 119  w | 151  . | 183  · | 215  × | 247  ÷ |
  67. |  24 ^X |  56  8 |  88  X | 120  x | 152  . | 184  ¸ | 216  Ø | 248  ø |
  68. |  25 ^Y |  57  9 |  89  Y | 121  y | 153  . | 185  ¹ | 217  Ù | 249  ù |
  69. |  26 ^Z |  58  : |  90  Z | 122  z | 154  . | 186  º | 218  Ú | 250  ú |
  70. |  27 ^[ |  59  ; |  91  [ | 123    | 155  . | 187  » | 219  Û | 251  û |
  71. |  28 ^\ |  60  < |  92  \ | 124  | | 156  . | 188  ¼ | 220  Ü | 252  ü |
  72. |  29 ^] |  61  = |  93  ] | 125    | 157  . | 189  ½ | 221  Ý | 253  ý |
  73. |  30 ^^ |  62  > |  94  ^ | 126  ~ | 158  . | 190  ¾ | 222  Þ | 254  þ |
  74. |  31 ^_ |  63  ? |  95  _ | 127   | 159  . | 191  ¿ | 223  ß | 255  ÿ |
  75.  
  76.     After character 127 the characters do vary depending on which
  77.     accented national characters are being used.  I left out 123 and 
  78.     125 because they are the opening and closing curly bracket
  79.     characters!  That would really mess up our comments,and
  80.     possibly our hair as well...
  81.  
  82.     ------------------------------
  83.  
  84.     STRING: A sequence of characters with a dynamic length within a
  85.         fixed maximum length.  The length can range from zero to
  86.         255.  Strings may be compared like numbers.  We met
  87.         strings before and we'll meet them again.
  88.  
  89.     ------------------------------
  90.  
  91.     REAL: A real number has fractional parts, and a much greater
  92.           range than the integer types.  For example...
  93.  
  94.             Real        Integer
  95.             ----        -------
  96.  
  97.             4.500        4
  98.  
  99.             -5        -5
  100.  
  101.             1.2e3        1200
  102.  
  103.     The "e" indicates exponent to the base 10, so that 1.2e3 is the
  104.     same as 1.2 x 10³, which is 1.2 x 1000 or 1200.  Clear as mud!
  105.  
  106.     HiSpeed Pascal supports the following REAL type definitions...
  107.  
  108.     Single = 3.4e-38..3.4e38
  109.  
  110.     Double = 1.7e-308..1.7e308  (This is the same as typing "Real")
  111.  
  112.     Extended = 1.1e-4932..1.1e4932
  113.  
  114.     Remember that these ranges are positive as well as negative, so if
  115.     you ever have a need for a negative number with 4932 zeros after
  116.     it, you know where to go (a psychiatrist?).        
  117.  
  118.     ------------------------------
  119.  
  120.     There are some other simple variable types, but we'll go into them
  121.     in another tutorial.  Right now, I'd like to use the variable type
  122.     REAL in a program which does some simple calculations.  Let's make
  123.     a program that takes the radius of a circle from the user and
  124.     calculates the diameter, area and circumference of that circle.  If
  125.     you're a bit of a maths quamby, take a look at Diagram_two for a
  126.     refresher course!  }
  127.  
  128.     Program MDTute5 (input,output);
  129.  
  130.         {       This program uses "real" numbers to do some simple
  131.  
  132.         mathematical calculations with circles
  133.  
  134.                 Author : A N Peck
  135.  
  136.                 Date : 19 June 1994 }
  137.  
  138.     const
  139.  
  140. {    ^
  141.     |
  142.     |__  This is another reserved word that allows you to put some
  143.          constants in at the start of the program.  The advantage is
  144.          two-fold.  Firstly, everytime you need the constant in the
  145.          program, you can refer to your previous definition.  Secondly,
  146.          if you want to change the constant, you need only alter it at
  147.          the start of the program, and it will be substituted in all
  148.          the way through (useful for really long programs).  }
  149.  
  150.     pi = 3.14159;
  151.  
  152. {    ^  ^ ^      ^
  153.     |  | |      |
  154.     |  | |      |__  A semi-colon to end each constant definition!
  155.     |  | |      
  156.     |  | |__  After the equals sign comes the actual constant.  Now,
  157.     |  |      whenever the compiler sees "pi" in the program, it will
  158.     |  |      substitute 3.14159.  If we need to change this later to
  159.     |  |      add precision, we only need to change the constant
  160.     |  |      definition.  Bogus!
  161.     |  |
  162.     |  |__  We let the compiler know what the constant is with an
  163.     |       "equals" sign.
  164.     |
  165.     |__  The constant name comes first, in this instance we are
  166.          defining the number that the word "pi" will represent.  }
  167.     
  168.     tab = '     ';
  169.  
  170. {    ^
  171.     |
  172.     |__  A constant doesn't have to be a number.  Here we've defined
  173.          that the word "tab" will be equal to five blank spaces.  We
  174.          can then use the "tab" for text formatting.  }
  175.  
  176.     var
  177.  
  178.     radius,diameter,circumference,area: real;
  179.  
  180. {    ^                    ^
  181.     |                    |
  182.     |                    |__  The variable type, in
  183.     |                     this case "real".
  184.     |                     We could have used
  185.     |                         "single","double" or
  186.     |                     "extended".  Now the user
  187.     |                     can input numbers with
  188.     |                     decimal points, and be
  189.     |                     given answers to that same
  190.     |                     precision!
  191.     |
  192.     |__  Because all the variables are of the same type, we can list
  193.          them together like this, separating them with commas.  It
  194.          would be just as accurate to list them like...
  195.  
  196.             var
  197.  
  198.             radius: real;
  199.  
  200.             diameter: real;
  201.  
  202.             etc.,...
  203.  
  204.          ...but why make work for ourselves?  }
  205.  
  206.     begin
  207.     
  208.     writeln;
  209.  
  210. {    ^
  211.     |
  212.     |__  We'll start with a blank line to make the output look pretty!
  213.          This is called programming as a fashion statement... }
  214.  
  215.     write(tab,'Please enter the radius of your circle: ');
  216.  
  217. {          ^
  218.           |
  219.           |__  Here's our first constant popping up!  When the
  220.            compiler sees this word "tab" it will insert the five
  221.            blank spaces that we have defined above.  Cool!  }
  222.  
  223.     readln(radius);
  224.  
  225.     diameter := radius*2;
  226.  
  227. {         ^      ^
  228.          |      |
  229.          |      |__  We use this to indicate multiplication. 
  230.          |           Some of the mathematical operations
  231.          |           available are represented as follows...
  232.          |
  233.          |        Addition:    +    e.g. 3 + 4 = 7
  234.          |
  235.          |        Subtraction:    -    e.g. 10 - 2 = 8
  236.          |
  237.          |        Multiplication:    *    e.g. 4.3 * 2 = 8.6
  238.          |
  239.          |        Division:    /    e.g. 8 / 4 = 2
  240.          |
  241.          |__  Yikes!  What is this?  This sign is the same as
  242.               saying "is equal to", but a bit more forceful!  We
  243.               are telling the compiler that the variable "diameter"
  244.               WILL be made equal to the "radius" variable
  245.               multiplied by two.  Note how it differs from the
  246.               normal equals sign used for constant declarations. 
  247.               We'll see other equals signs in another tutorial
  248.               (gulp!).  }
  249.  
  250.     circumference := diameter*pi;
  251.  
  252. {                  ^
  253.                   |
  254.                   |__  Here's the other constant!  }
  255.  
  256.     area := pi*radius*radius;
  257.  
  258. {        ^
  259.         |
  260.         |__  There are different ways to approach this.  The
  261.              formula for area of a circle is A = pi x r².  Instead
  262.              of writing the r² bit as radius*radius, pascal has an
  263.              inbuilt squaring function called SQR.  So we could
  264.              have written it as...
  265.  
  266.              area := pi*SQR(radius);
  267.  
  268.              I'm just lazy and also a strong believer in the
  269.              K.I.S.S. principle (Keep It Simple Stupid).  }
  270.  
  271.     writeln;
  272.  
  273.     writeln(tab,'Dimensions for a circle of radius',radius:0:2,':');
  274.  
  275. {                                  ^ ^
  276.                                   |    |
  277.         Real variables give their full precision,   __|    |
  278.         but often we only need the answer to two    |
  279.         decimal places.  This first colon tells the    |
  280.         the compiler that some formatting information    |
  281.         is about to follow.  The zero indicates that    |
  282.         we want zero places between the last thing    |
  283.         written, and the start of the number.  We'll    |
  284.         see how useful this is when formatting         |
  285.         columns in a later tutorial.            |
  286.                                 |
  287.         The second colon indicates that a number      __|
  288.         representing the precision is about to follow,    
  289.         in this instance we are requesting that the    
  290.         variable "radius" be shown to just two        
  291.         decimal places.  Radical!
  292.  
  293.     This can be a confusing point so I'll elaborate.  Let's say that
  294.     we have a variable "radius" and the user gives us the value 4.567. 
  295.     Here are some of the possible formats we can use...
  296.  
  297.     writeln(radius)        gives    4.567000000000000E+000
  298.  
  299.     writeln(radius:10:0)    gives        5
  300.  
  301.     writeln(radius:8:2)    gives       4.57
  302.  
  303.     writeln(radius:2:3)    gives    4.567
  304.  
  305.     writeln(radius:0:5)    gives    4.56700
  306.  
  307.     Notice how the first one, with no input from us on formatting, is
  308.     totally out of control!  The second places the number 10 places in
  309.     from the edge, but allows no decimal points.  In fact, it rounds
  310.     off the number given as well (bonus!).  Also note the extra
  311.     precision given to the last format.  Awesome!
  312.  
  313.     The first number is left formatting so the command 
  314.     writeln(radius:2:3) is no different than writeln(radius:0:3).  
  315.     You'll see this more clearly when we come to columns!
  316.  
  317.     Note that even though I don't ask for a space (writeln(radius:0:2)),
  318.     I've left room for one by not putting in a blank after (...radius',).
  319.     This is because when dealing with real numbers, you need to take
  320.     into account the sign!  Although not shown for positive numbers, an
  321.     answer like 56.75 is really +56.75.  If you're not sure on this, run
  322.     the program with a negative radius.
  323.  
  324.     Have a play with this formatting until you either understand it,
  325.     or feel like ripping your own head off.  }    
  326.  
  327.     writeln;
  328.  
  329.     writeln(tab,'Diameter:',diameter:0:2);
  330.  
  331. {                    ^
  332.                     |
  333.                     |__  We can format the resultant
  334.                          answer given to us by the
  335.                          calculation we defined above. 
  336.                          All of these answers will have
  337.                          a precision of two decimal
  338.                          points.  }
  339.  
  340.     writeln(tab,'Circumference:',circumference:0:2);
  341.  
  342.     writeln(tab,'Area:',area:0:2);
  343.  
  344.     writeln;
  345.  
  346.     end.
  347.  
  348. {    Wow!  Fantastic.  What sort of debugging should we do on a program
  349.     like this?  Try some of the following...
  350.  
  351.     * Enter the radius as 0, 0.0, 00.00, etc.,.
  352.  
  353.     * Try a negative radius (twilight zone circles).
  354.  
  355.     * When asked for the radius, type in your name!
  356.  
  357.     * Enter a radius with a precision greater than that displayed, for
  358.       example try 0.004
  359.  
  360.     * Try a VERY big number, like 47385627847264...
  361.  
  362.     * Can you enter in an exponent?  Try 3.4e4, or 2e-1.
  363.  
  364.     It's always a good idea to get another person to look at the
  365.     finished product because they're sure to find some flaws where you
  366.     see only perfection.
  367.  
  368.         ----------------------END OF TUTE5.PAS----------------------  }
  369.  
  370.  
  371.